package com.soft.small.util.decrypt;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.soft.util.encrypt.Aes;


public class DecryptResponse {

    public static String parseResponse(String result ,String appSecret) {
        if (result==null || result.length()<1){
            return null;
        }
        try {
            JSONObject resultJson = JSONObject.parseObject(result);
            if (resultJson!= null){
                String code = resultJson.getString("code");
                if (code.equals("0")) {
                    String dataStr = resultJson.getString("data");
                    if (dataStr != "" && dataStr != null&&!dataStr.equals("null")&&!dataStr.equals("{}")){
                        String decryptDataStr = Aes.decryptAes(dataStr, appSecret);
                        if(decryptDataStr != null&&!decryptDataStr.equals("null")&&!decryptDataStr.equals("")){
                            if (decryptDataStr.startsWith("{")){
                                JSONObject decryptDataJson = JSONObject.parseObject(decryptDataStr);
                                resultJson.put("data", decryptDataJson);
                            }
                            if (decryptDataStr.startsWith("[")) {
                                JSONArray decryptDataJson = JSONArray.parseArray(decryptDataStr);
                                resultJson.put("data", decryptDataJson);
                            }
                        }

                    }
                }
            }
            return resultJson.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
